<?php

namespace app\store\controller\content;

use app\store\controller\Controller;
use app\store\model\Article as ArticleModel;
use app\store\model\article\Category as CategoryModel;
/**
 * 文章管理控制器
 * Class article
 * @package app\store\controller\content
 */
class Article extends Controller
{
    /**
     * 文章列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($category_id = null , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = 10005,$not_aritcle_id = 10038);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    /**
     * 文章列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function qita($category_id = 10015 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        // 单独查 article_id = 10038 的文章（不管它的分类是啥）
        $article10038 = $model
            ->with(['image', 'category']) // 保持结构一致
            ->where('is_delete', 0)
            // ->where('article_id', 10038)
            ->find();

        // 如果查到了，就插入到列表开头（或结尾）
        if ($article10038) {
            // $list->unshift($article10038); // 插入到最前面，可改为 push() 放最后
        }
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function ct($category_id = 10007 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function wc($category_id = 10009 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function ss($category_id = 10010 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function jx($category_id = 10011 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function nkj($category_id = 10008 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function jyjc($category_id = 10012 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function fzzd($category_id = 10013 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function kfll($category_id = 10014 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    public function weichuang($category_id = 10007 , $start_time = null,$end_time = null)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id,$start_time,$end_time,$not_category_id = null);
        $model1 = new CategoryModel;
        $catgory = $model1->getAll();
        return $this->fetch('index', compact('list','catgory'));
    }
    /**
     * 添加文章
     * @return array|mixed
     */
    public function add()
    {
        $model = new ArticleModel;
        if (!$this->request->isAjax()) {
            // 文章分类
            $catgory = CategoryModel::getAll();
            return $this->fetch('add', compact('catgory'));
        }
        // 新增记录
        if ($model->add($this->postData('article'))) {
            return $this->renderSuccess('添加成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新文章
     * @param $article_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($article_id)
    {
        // 文章详情
        $model = ArticleModel::detail($article_id);
        // dump($model->toArray());die;
        if (!$this->request->isAjax()) {
            // 文章分类
            $catgory = CategoryModel::getAll();
            return $this->fetch('edit', compact('model', 'catgory'));
        }
        $data = $this->postData('article');

        // 如果客户端没传 image_id，就说明图片被删除了
        if (!isset($data['image_id']) || empty($data['image_id'])) {
            $data['image_id'] = null; // 或者 null，取决于你的表定义
        }

        if ($model->edit($data, $article_id)) {
            return $this->renderSuccess('更新成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章
     * @param $article_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($article_id)
    {
        // 文章详情
        $model = ArticleModel::detail($article_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}